<template>
  <div style="position: relative; width: 100vw">
    <el-row justify="center" style="padding-top: 5vh">
      <el-col :span="20">
        <el-card class="card">
          <el-form ref="addOrEditFormRef" :model="formData" :rules="rules" label-width="180">
            <el-row style="margin-left: 50px; text-indent: 2em; line-height: 30px; font-weight: 600"
              ><el-col align="center"
                >审核失败原因: <el-text type="danger">{{ formData.auditReason }}</el-text></el-col
              ></el-row
            >
            <el-row
              style="
                border-left: 5px solid #3b98f7;
                margin-left: 50px;
                text-indent: 2em;
                line-height: 30px;
                font-weight: 600;
              "
              >施工总承包信息</el-row
            >
            <!-- 所属施工企业信息 hzj 2023/11/6-->
            <el-row justify="start" style="margin-top: 10px">
              <el-col :span="itemSpan" style="position: relative">
                <el-form-item label="施工总承包" prop="lumpContractorCorpId">
                  <el-input
                    v-model="formData.lumpContractorCorpName"
                    readonly
                    placeholder="请点击选择施工总承包"
                    class="input300 inputred"
                    @click="openZBCorpSelect"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="itemSpan">
                <el-form-item label="施工总承包信用代码" prop="lumpContractorCorpCode">
                  <el-input
                    v-model="formData.lumpContractorCorpCode"
                    readonly
                    disabled
                    placeholder="请选择施工总承包"
                    class="input300"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="itemSpan">
                <el-form-item label="施工总承包联系电话" prop="lumpContractorCorpPhoneNum">
                  <el-input
                    v-model="formData.lumpContractorCorpPhoneNum"
                    placeholder="请输入施工总承包联系电话"
                    class="input300"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <project-info-form
            v-if="formData.projectId != undefined"
            ref="projectInfoFormRef"
            @current-project="GetCurrentProject"
          ></project-info-form>
          <SelectCorp
            v-if="dialogSelectZBCorp.visible"
            :dialog-select-corp="dialogSelectZBCorp"
            :corp-list-query="'common'"
            @zb-corp-send-close="zbCorpSendClose"
          >
          </SelectCorp>
          <el-row style="padding: 15px">
            <el-col align="center"
              ><el-button type="primary" :disabled="disableSubmit" @click="handleSubmit">提交重新审核</el-button>
              <el-button type="primary" @click="goBack">返回登录</el-button></el-col
            >
          </el-row>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { useRoute } from "vue-router";
import { isMobile } from "@/utils";
import { Except, Ride } from "@/utils/decimal";
import { GetFailProjectForm, FailProjectreSubmit } from "@/api/common/project/index";
import ProjectInfoForm from "@/views/account/register/project/projectInfo.vue";
import { ProjectCheckInForm } from "@/api/project/projectInfo/types";
import { NewProjectManagerType } from "@/api/manager/project/types";
import { useScreen } from "@/store/modules/screen";
import { FormRules } from "element-plus";
import SelectCorp from "@/views/manager/common/SelectCorp/index.vue";
import router from "@/router";

const route = useRoute();
const editToken = ref<any>(route.query.editToken);
const disableSubmit = ref<boolean>(true);

//声明变量
const screenStore = useScreen();
const mobile = computed(() => screenStore.device);
const screenWidth = ref(900);
const itemSpan = ref(12);
const formData = reactive<NewProjectManagerType>({});
const rules = reactive<FormRules<NewProjectManagerType>>({
  lumpContractorCorpId: [{ required: true, message: "请选择施工企业", trigger: "blur" }],
  lumpContractorCorpCode: [
    {
      required: true,
      message: "请输入施工总承包社会信用代码",
      trigger: "blur",
    },
  ],
  lumpContractorCorpPhoneNum: [{ required: false, message: "请输入施工总承包联系电话", trigger: "blur" }],
});
const dialogSelectZBCorp = reactive<DialogOption>({
  visible: false,
  title: "选择施工企业",
  id: 0,
});
/* 关闭总包企业选择 */
function zbCorpSendClose(corpInfo: { corpId: number; name: string; code: string }) {
  if (corpInfo) {
    formData.lumpContractorCorpId = corpInfo.corpId;
    formData.lumpContractorCorpName = corpInfo.name;
    formData.lumpContractorCorpCode = corpInfo.code;
  }
  dialogSelectZBCorp.visible = false;
}
/**选择施工企业 */
function openZBCorpSelect() {
  dialogSelectZBCorp.visible = true;
}
const emit = defineEmits(["sendClose"]);
const loading = ref(false);
const addOrEditFormRef = ref(ElForm);
const projectInfoFormRef = ref(ElForm);

//获取当前项目信息
const GetCurrentProject = function (callback: Function) {
  callback(formData.projectId, false);
};

const goBack = () => {
  router.push("/login");
};

function initScreen() {
  if (mobile.value === "mobile") {
    screenWidth.value = screenStore.screenWidth;
    itemSpan.value = 24;
  } else {
    screenWidth.value = 1200;
    itemSpan.value = 8;
  }
}

const initForm = async function () {
  if (editToken) {
    loading.value = true;
    let res = await GetFailProjectForm(editToken.value);
    loading.value = false;
    if (res.type == 0 && res.success) {
      Object.assign(formData, res.data);
      disableSubmit.value = false;
    }
  }
};
onMounted(async () => {
  initScreen();
  initForm();
});

/**
 * 项目表单提交
 */
const handleSubmit = async () => {
  let valid = await addOrEditFormRef.value.validate();
  if (!valid) {
    return;
  }
  let valid2 = await projectInfoFormRef.value.validation();
  if (!valid2) {
    return;
  }

  loading.value = true;

  if (projectInfoFormRef.value.fileList.length > 0) {
    projectInfoFormRef.value.formData.attachments = projectInfoFormRef.value.fileList.map((item: any) => {
      if (item.status === "success") {
        if (item.response) {
          return item.response.data;
        } else {
          return item.uid;
        }
      }
    });
  }
  if (projectInfoFormRef.value.noSupervisorFileList.length > 0) {
    projectInfoFormRef.value.formData.noSupervisorAttachments = projectInfoFormRef.value.noSupervisorFileList.map(
      (item: any) => {
        if (item.status === "success") {
          if (item.response) {
            return item.response.data;
          } else {
            return item.uid;
          }
        }
      }
    );
  }
  if (projectInfoFormRef.value.positionFileList.length > 0) {
    projectInfoFormRef.value.formData.positionAttachments = projectInfoFormRef.value.positionFileList.map(
      (item: any) => {
        if (item.status === "success") {
          if (item.response) {
            return item.response.data;
          } else {
            return item.uid;
          }
        }
      }
    );
  }
  if (projectInfoFormRef.value.earnestFileList.length > 0) {
    projectInfoFormRef.value.formData.earnestAttachments = projectInfoFormRef.value.earnestFileList.map((item: any) => {
      if (item.status === "success") {
        if (item.response) {
          return item.response.data;
        } else {
          return item.uid;
        }
      }
    });
  }
  let submitForm = Object.assign({}, projectInfoFormRef.value.formData);
  submitForm.contractAmount = Ride(submitForm?.contractAmount ?? 0, 10000);
  submitForm.earnestMoney = Ride(submitForm?.earnestMoney ?? 0, 10000);
  submitForm.supervisorCorpAmount = Ride(submitForm?.supervisorCorpAmount ?? 0, 10000);
  submitForm.lumpContractorCorpId = formData.lumpContractorCorpId;
  submitForm.lumpContractorCorpPhoneNum = formData.lumpContractorCorpPhoneNum;
  submitForm.editToken = editToken.value;
  FailProjectreSubmit(submitForm)
    .then((res) => {
      if (res.type == 0 && res.success) {
        ElMessage.success(res.msg);
        disableSubmit.value = true;
      }
    })
    .finally(() => (loading.value = false));
};
</script>
<style lang="scss" scoped>
.d_left {
  margin-left: 60px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}

:deep(.el-input) {
  width: 200px;
}
:deep(.el-input-number) {
  width: 200px;
}

:deep(.el-input__wrapper) {
  width: 200px;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
